package com.tangw.manage.action;

import javax.servlet.http.HttpSession;

import com.tangw.manage.dao.UserDao;
import com.tangw.manage.dao.impl.UserDaoImpl;
import com.tangw.manage.model.User;

/**
 * 
 * @author TangW
 * 
 */
@SuppressWarnings("serial")
public class UserAction extends BaseAction {

	private UserDao dao = new UserDaoImpl();

	private User user = new User();

	public String index() {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			// code
			return "index_success";
		}
		return "index_error";
	}

	public String login() {
		String username = user.getUsername();
		String password = user.getPassword();
		request.removeAttribute("errorMsg");
		User user = null;
		if (username != null && password != null && !username.isEmpty()
				&& !password.isEmpty()) {
			try {
				user = dao.getUserByUsernameAndPassword(username, password);
				if (user != null) {
					HttpSession session = request.getSession();
					session.setAttribute("user", user);
					return "login_success";
				}
				request.setAttribute("errorMsg", "帐号或密码错误！");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "login_error";
	}

	public String regist() {
		return "regist_success";
	}

	public String logout() {

		return "";
	}

	public String update() {

		return "";
	}

	public String delete() {

		return "";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserDao getDao() {
		return dao;
	}

	public void setDao(UserDao dao) {
		this.dao = dao;
	}

}
